package com.fingard.dsp.bank.directbank.cmbunion01;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;

import com.alipay.api.internal.util.AlipaySignature;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.directbank.cmbunion01.util.BoingPay;
import com.fingard.dsp.bank.directbank.cmbunion01.util.DSAUtil;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;

public class CMBUnion01OtherElse extends CMBUnion01Base implements IOtherElse {
	private static PublicKey DSAPublicKey;
	@Override
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		try{
			String transType = p_reqElse.reqHeader.transType;
			if ("VerifySign".equals(transType)) {
				String ownAct = "";
				if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
					ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
				}
				String plain = "";
				if (p_reqElse.reqHashMap.containsKey("Plain")) {
					plain = StringEscapeUtils.unescapeHtml(p_reqElse.reqHashMap.get("Plain"));
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", "缺失验签原始串");

					return retAts;
				}
				WriteBankLogLn("招商聚合支付通知报文："+plain);
				ActSetItem actItem = this.getBankConfig().getActSetItem(ownAct, "");
				if (actItem != null) {
					plain = plain.trim();
			        Map<String, String> params = convertToMap(plain.substring(0,plain.length()-1));
					String[] RSApath = actItem.ownCertFilePath.split(",");
					setConfig(actItem.merchantId,bankFront.getUrl("unifiedPay"),RSApath[0],RSApath[1],actItem.ownKeyStorePath,actItem.oppCertFilePath,actItem.ownKeyPassword);
					String signStr = (String)params.get("app_id") + (String)params.get("timestamp") + (String)params.get("version") + (String)params.get("event");
			        String sign = (String)params.get("sign");
			        boolean isVerify= false;
			        DSAPublicKey = DSAUtil.loadPublicKey(actItem.ownKeyStorePath);
			        if ((sign != null) && (!"".equals(sign))) {
			        	isVerify =  DSAUtil.verify(DSAPublicKey, signStr, sign);
			        	WriteBankLogLn("-招商聚合验签结果："+isVerify);
			        }
					//boolean signVerified = BoingPay.verifySign(params);
			        retAts.retHashMap = new HashMap<String, String>();
					if (isVerify) {
						retAts.retHashMap.put("RespCode", "0");
						retAts.retHashMap.put("RespInfo", "验签通过");
					} else {
						retAts.retHashMap.put("RespCode", "-1");
						retAts.retHashMap.put("RespInfo", "验签失败");
					}
					WriteBankLogLn("-招商聚合验签结果1："+retAts);
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
				}
			}else {

				retAts.retHashMap = new HashMap<String, String>();
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);

				
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}
		return retAts;
	}

}
